<template>
	<view class="countdown-page">
		<view class="back-btn" @click="goBack">
			<image src="/static/images/back.png" mode="aspectFit"></image>
		</view>

		<view class="countdown-container">
			<view class="time-display" :style="{fontSize: timeFontSize + 'px'}">
				{{ formattedTime }}
			</view>

			<view class="controls">
				<view class="btn start" @click="startCountdown" v-if="!isRunning">
					<!-- <image src="/static/images/play.png" mode="aspectFit"></image> -->
					<text>开始</text>
				</view>
				<view class="btn pause" @click="pauseCountdown" v-else>
					<!-- <image src="/static/images/pause.png" mode="aspectFit"></image> -->
					<text>暂停</text>
				</view>
				<view class="btn reset" @click="resetCountdown">
					<!-- <image src="/static/images/reset.png" mode="aspectFit"></image> -->
					<text>重置</text>
				</view>
			</view>

			<view class="time-set">
				<view class="time-selector">
					<view class="time-label">分钟</view>
					<view class="time-wheel" @touchstart="startDrag('minutes')" @touchmove.prevent="handleDrag" @touchend="endDrag">
						<view class="time-value">{{ minutes }}</view>
						<view class="time-controls">
							<view class="time-up" @click.stop="adjustTime('minutes', 1)">↑</view>
							<view class="time-down" @click.stop="adjustTime('minutes', -1)">↓</view>
						</view>
					</view>
				</view>
				
				<view class="time-selector">
					<view class="time-label">秒钟</view>
					<view class="time-wheel" @touchstart="startDrag('seconds')" @touchmove.prevent="handleDrag" @touchend="endDrag">
						<view class="time-value">{{ seconds }}</view>
						<view class="time-controls">
							<view class="time-up" @click.stop="adjustTime('seconds', 1)">↑</view>
							<view class="time-down" @click.stop="adjustTime('seconds', -1)">↓</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="alarm" v-if="showAlarm">
			<image src="/static/images/alarm.png" mode="aspectFit" class="alarm-icon"></image>
			<text class="alarm-text">时间到!</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				totalSeconds: 300, // 默认5分钟
				minutes: 5,
				seconds: 0,
				isRunning: false,
				timer: null,
				showAlarm: false,
				timeFontSize: 120,
				dragging: false,
				dragStartY: 0,
				dragType: '',
				dragValue: 0
			}
		},
		computed: {
			formattedTime() {
				const mins = Math.floor(this.totalSeconds / 60)
				const secs = this.totalSeconds % 60
				return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
			}
		},
		onLoad() {
			this.adjustFontSize()
		},
		onUnload() {
			clearInterval(this.timer)
		},
		methods: {
			adjustFontSize() {
				const { windowWidth } = uni.getSystemInfoSync()
				this.timeFontSize = windowWidth * 0.15
			},
			startCountdown() {
				if (this.totalSeconds <= 0) return

				this.isRunning = true
				this.showAlarm = false

				this.timer = setInterval(() => {
					if (this.totalSeconds <= 0) {
						this.finishCountdown()
						return
					}
					this.totalSeconds--
				}, 1000)
			},
			pauseCountdown() {
				this.isRunning = false
				clearInterval(this.timer)
			},
			resetCountdown() {
				this.pauseCountdown()
				this.totalSeconds = (this.minutes * 60) + parseInt(this.seconds || 0)
				this.showAlarm = false
			},
			finishCountdown() {
				clearInterval(this.timer)
				this.isRunning = false
				this.showAlarm = true

				uni.vibrateLong({
					success: () => {
						console.log('震动成功')
					}
				})

				setTimeout(() => {
					this.showAlarm = false
				}, 5000)
			},
			updateTotalSeconds() {
				this.totalSeconds = (this.minutes * 60) + parseInt(this.seconds || 0)
			},
			goBack() {
				this.pauseCountdown()
				uni.navigateBack()
			},
			// 点击调整时间
			adjustTime(type, delta) {
				if (type === 'minutes') {
					this.minutes = Math.max(0, Math.min(120, this.minutes + delta))
				} else {
					this.seconds = Math.max(0, Math.min(59, this.seconds + delta))
				}
				this.updateTotalSeconds()
			},
			// 开始拖动
			startDrag(type) {
				this.dragging = true
				this.dragType = type
				this.dragValue = type === 'minutes' ? this.minutes : this.seconds
			},
			// 处理拖动
			handleDrag(e) {
				if (!this.dragging) return
				
				const touch = e.touches[0]
				if (!this.dragStartY) {
					this.dragStartY = touch.clientY
					return
				}
				
				const deltaY = touch.clientY - this.dragStartY
				const deltaValue = Math.round(deltaY / 20) * -1 // 每20像素变化1个单位
				
				if (this.dragType === 'minutes') {
					this.minutes = Math.max(0, Math.min(120, this.dragValue + deltaValue))
				} else {
					this.seconds = Math.max(0, Math.min(59, this.dragValue + deltaValue))
				}
				this.updateTotalSeconds()
			},
			// 结束拖动
			endDrag() {
				this.dragging = false
				this.dragStartY = 0
				this.dragValue = 0
			}
		}
	}
</script>

<style lang="scss" scoped>
	.countdown-page {
		width: 100%;
		height: 100vh;
		background: linear-gradient(135deg, #ffd6edb3 0%, #aeceff 100%);
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative;
		overflow: hidden;

		.back-btn {
			position: absolute;
			top: 40rpx;
			left: 40rpx;
			width: 60rpx;
			height: 60rpx;
			z-index: 10;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.countdown-container {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			width: 90%;
			max-width: 800rpx;
			background: rgba(255, 255, 255, 0.9);
			border-radius: 30rpx;
			padding: 50rpx;
			box-shadow: 0 20rpx 50rpx rgba(0, 0, 0, 0.1);
			position: relative;
			z-index: 1;

			&::before {
				content: '';
				position: absolute;
				top: -10rpx;
				left: -10rpx;
				right: -10rpx;
				bottom: -10rpx;
				border-radius: 40rpx;
				background: linear-gradient(45deg, #4facfe 0%, #00f2fe 100%);
				z-index: -1;
				opacity: 0.6;
				filter: blur(20rpx);
			}
		}

		.time-display {
			font-family: 'Arial', sans-serif;
			font-weight: bold;
			color: #333;
			margin: 40rpx 0;
			text-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
			transition: all 0.3s ease;
		}

		.controls {
			display: flex;
			justify-content: center;
			margin: 40rpx 0;

			.btn {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				width: 150rpx;
				height: 150rpx;
				border-radius: 50%;
				margin: 0 30rpx;
				background: white;
				box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
				transition: all 0.3s ease;

				&:active {
					transform: scale(0.95);
				}

				image {
					width: 60rpx;
					height: 60rpx;
					margin-bottom: 10rpx;
				}

				text {
					font-size: 28rpx;
					color: #333;
				}
			}

			.start {
				background: linear-gradient(45deg, #4facfe 0%, #00f2fe 100%);

				text {
					color: white;
				}
			}

			.pause {
				background: linear-gradient(45deg, #f093fb 0%, #f5576c 100%);

				text {
					color: white;
				}
			}

			.reset {
				background: linear-gradient(45deg, #43e97b 0%, #38f9d7 100%);

				text {
					color: white;
				}
			}
		}

		.time-set {
			display: flex;
			justify-content: center;
			margin-top: 30rpx;
			width: 100%;

			.time-selector {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin: 0 40rpx;
				
				.time-label {
					font-size: 28rpx;
					color: #666;
					margin-bottom: 10rpx;
				}
				
				.time-wheel {
					width: 120rpx;
					height: 200rpx;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					background: white;
					border-radius: 20rpx;
					box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.1);
					position: relative;
					overflow: hidden;
					
					.time-value {
						font-size: 40rpx;
						font-weight: bold;
						color: #333;
						margin: 20rpx 0;
					}
					
					.time-controls {
						display: flex;
						flex-direction: column;
						align-items: center;
						width: 100%;
						
						.time-up, .time-down {
							width: 100%;
							height: 40rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							font-size: 30rpx;
							color: #4a90e2;
							background: rgba(74, 144, 226, 0.1);
							
							&:active {
								background: rgba(74, 144, 226, 0.2);
							}
						}
					}
				}
			}
		}

		.alarm {
			position: absolute;
			top: 40%;
			left: 50%;
			width: 100%;
			transform: translate(-50%, -50%);
			display: flex;
			flex-direction: column;
			align-items: center;
			z-index: 100;
			animation: alarm 0.5s infinite alternate;

			.alarm-icon {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 20rpx;
			}

			.alarm-text {
				font-size: 400rpx;
				font-weight: bold;
				color: #f5576c;
				text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.2);
			}
		}
	}

	@keyframes alarm {
		from {
			transform: translate(-50%, -50%) scale(1);
		}

		to {
			transform: translate(-50%, -50%) scale(1.1);
		}
	}

	/* 横屏适配 */
	@media (orientation: landscape) {
		.countdown-page {
			.countdown-container {
				width: 80%;
				max-width: none;
				padding: 30rpx;
			}

			.time-display {
				font-size: 100px !important;
				margin: 20rpx 0;
			}

			.controls {
				margin: 20rpx 0;

				.btn {
					width: 120rpx;
					height: 120rpx;
					margin: 0 20rpx;

					image {
						width: 50rpx;
						height: 50rpx;
					}
				}
			}
			
			.time-set {
				.time-selector {
					margin: 0 20rpx;
					
					.time-wheel {
						width: 100rpx;
						height: 200rpx;
						
						.time-value {
							font-size: 36rpx;
						}
					}
				}
			}
		}
	}
</style>